import React, { Component } from 'react';
import PropTypes from 'prop-types'
import './index.css'

class Index extends Component {
  static propTypes = {
    todos: PropTypes.array.isRequired,
    checkAll: PropTypes.func.isRequired,
    handleClearDone: PropTypes.func.isRequired,
  }

  handleCheckAll = (event) => {
    this.props.checkAll(event.target.checked)
  }

  handleClearAllDone = () => {
    this.props.handleClearDone()
  }
  render() {
    const { todos } = this.props
    // 已完成数
    // const doneCount = todos.filter(item => item.done).length
    const doneCount = todos.reduce((pre, cur) => cur.done ? pre + 1 : pre, 0)

    // 总数
    const total = todos.length
    return (
      <div className="todo-footer">
        <label>
          <input type="checkbox" checked={doneCount === total && total !== 0 ? true : false} onChange={this.handleCheckAll} />
        </label>
        <span>
          <span>已完成 {doneCount}</span> / 全部 {total}
        </span>
        <button  className="btn btn-danger" onClick={this.handleClearAllDone}>清除已完成任务</button>
      </div>
    );
  }
}

export default Index;
